/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.course.entity.CourseChapterSubEntity;
import org.springblade.modules.course.entity.SubBankAnswerOptionEntity;
import org.springblade.modules.course.entity.SubVideoReadEntity;
import org.springblade.modules.course.service.ISubBankAnswerOptionService;
import org.springblade.modules.course.service.ISubVideoReadService;
import org.springblade.modules.course.vo.CourseChapterSubVO;
import org.springblade.modules.course.mapper.CourseChapterSubMapper;
import org.springblade.modules.course.service.ICourseChapterSubService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.resource.enums.AttachType;
import org.springblade.modules.teaching.entity.CourseChapterSubBankEntity;
import org.springblade.modules.teaching.entity.CourseChapterSubFileEntity;
import org.springblade.modules.teaching.service.ICourseChapterSubBankService;
import org.springblade.modules.teaching.service.ICourseChapterSubFileService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 课程章节具体小节 服务实现类
 *
 * @author
 * @since 2023-03-22
 */
@Service
@AllArgsConstructor
public class CourseChapterSubServiceImpl extends BaseServiceImpl<CourseChapterSubMapper, CourseChapterSubEntity> implements ICourseChapterSubService {
	private final ISubBankAnswerOptionService subBankAnswerOptionService;
	private final ISubVideoReadService subVideoReadService;
	private final ICourseChapterSubBankService courseChapterSubBankService;
	private final ICourseChapterSubFileService courseChapterSubFileService;

	@Override
	public IPage<CourseChapterSubVO> selectCourseChapterSubPage(IPage<CourseChapterSubVO> page, CourseChapterSubVO courseChapterSub) {
		return page.setRecords(baseMapper.selectCourseChapterSubPage(page, courseChapterSub));
	}


	@Override
	public List<CourseChapterSubEntity> selectByChapterId(Long ChapterId) {
		LambdaQueryWrapper<CourseChapterSubEntity> lqw = Wrappers.<CourseChapterSubEntity>query().lambda().
			eq(CourseChapterSubEntity::getChapterId, ChapterId).orderByAsc(CourseChapterSubEntity::getSort);
		return baseMapper.selectList(lqw);
	}

	@Override
	public List<CourseChapterSubVO> queryBySubAndCategory(Long subId, Integer category) {
		return baseMapper.queryBySubAndCategory(subId, category);
	}

	@Override
	public CourseChapterSubVO queryCoursewareBySubAndCategory(Long subId, Integer category) {
		return baseMapper.queryCoursewareBySubAndCategory(subId, category);
	}

	@Override
	public boolean checkSubStudyComplete(Long userId, Long subId) {
		boolean flag = true;
		//检测有无学习视频
		long subVedioCount = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
			.eq(CourseChapterSubFileEntity::getSubId, subId).eq(CourseChapterSubFileEntity::getAttachType, AttachType.COURSE_VEDIO));

		//查询课后习题数量
		long subBankCount = courseChapterSubBankService.count(Wrappers.<CourseChapterSubBankEntity>query().lambda()
			.eq(CourseChapterSubBankEntity::getSubId, subId));

		/**
		 * 如果小节既没有课后习题，也没有学习视频
		 * 则直接表示已完成
		 */
		if (subVedioCount == 0 && subBankCount == 0) return true;

		/**
		 * 检测小节练习题是否完成
		 */
		if (subBankCount > 0) {
			long answerCount = subBankAnswerOptionService.count(Wrappers.<SubBankAnswerOptionEntity>query().lambda()
				.eq(SubBankAnswerOptionEntity::getUserId, userId).eq(SubBankAnswerOptionEntity::getSubId, subId));

			//课后习题要么有做，要么没有做，如果能查到做题记录 说明已经做完该小节的课后习题
			if (answerCount == 0) return false;
		}


		/**
		 * 检测视频是否观看完成
		 */
		if (subVedioCount > 0) {
			SubVideoReadEntity subVideoReadEntity = subVideoReadService.getOne(Wrappers.<SubVideoReadEntity>query().lambda().
				eq(SubVideoReadEntity::getSubId, subId).eq(SubVideoReadEntity::getUserId, userId));
			if (null == subVideoReadEntity) flag = false; //如果没有查看该用户此小节观看视频的完成记录
		}

		return flag;
	}


}
